package top.hkyzf.study.leetcode.string;

import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * description:
 *
 * @author zhufeng
 * @version 1.0
 * @date 2024-3-10 12:35
 */
public class 猜数字游戏_299 {
    public String getHint(String secret, String guess) {
        Map<Character, Integer> secretMap = new HashMap<>();
        Map<Character, Integer> guessMap = new HashMap<>();
        Set<Character> secretSet = new HashSet<>();
        Set<Character> guessSet = new HashSet<>();
        int bulls = 0;
        for (int i = 0; i < secret.length(); i++) {
            if (secret.charAt(i) == guess.charAt(i)) {
                bulls++;
            } else {
                secretMap.put(secret.charAt(i), secretMap.getOrDefault(secret.charAt(i), 0) + 1);
                guessMap.put(guess.charAt(i), guessMap.getOrDefault(guess.charAt(i), 0) + 1);
                secretSet.add(secret.charAt(i));
                guessSet.add(guess.charAt(i));
            }
        }
        secretSet.retainAll(guessSet);
        AtomicInteger cows = new AtomicInteger();
        secretSet.forEach(se -> {
            cows.addAndGet(Math.min(secretMap.get(se), guessMap.get(se)));
        });
        return bulls + "A" + cows + "B";
    }

    @Test
    public void testGetHint () {
        String secret = "1122";
        String guess = "2211";
        System.out.println(getHint(secret, guess));
    }
}
